<?php

class LinkAction extends BaseAction {

	public function _initialize(){
		parent::_initialize();
	
	}

    public function index(){

        import('ORG.Util.Page');// 导入分页类
    	$count  = D('links')->where('sq = 0')->count();  // 查询满足要求的总记录数
        $Pages   = new Page($count,25);           // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show   = $Pages->show();                        // 分页显示输出
        $list = D('links')->where('sq = 0')->limit($Pages->firstRow.','.$Pages->listRows)->select();
		foreach($list as $k => &$v){
			
			$url = U('Link/status',array('lid'=>$v['lid'],'key'=>$v['status']));
			$name  = ($v['status'] == 1)?'开启':'关闭';
			$color = ($v['status'] == 1)?'green': 'red';
			$v['status'] = '<a href="'.$url.'" style="color:'.$color.'">'.$name.'</a>';
		}
		$_SESSION['WKEIN_BACK_URL'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->assign('list',$list);                    // 赋值数据集
        $this->assign('page',$show);                    // 赋值分页输出
    	$this->show();
    }


    public function addLink(){
    	$acname  = '新增友情链接';  
        $posturl =  U('Link/doaddLink');
        $this->assign('acname',$acname);
        $this->assign('posturl',$posturl);
    	$this->show();
    }

    public function updateLink(){
    	$map['lid'] = I('get.lid');
    	$data = D('links')->where($map)->find();
    	$acname  = '修改友情链接';  
        $posturl =  U('Link/doupdateLink');
        $this->assign('acname',$acname);
        $this->assign('posturl',$posturl);
    	$this->assign('data',$data);
    	$this->show('addLink');
    }

    public function doaddLink(){
    	$info = I('post.');
    	$data = array('title'=>$info['title'],'url'=>$info['url'],'status'=>1,'time'=>time());
    	$add  = D('links')->add($data);
    	if($add){
    		$this->success('新增友情链接成功！',$_SESSION['WKEIN_BACK_URL']);
       	}else{
            $this->error('新增友情链接失败！',$_SESSION['WKEIN_BACK_URL']);
       	}
    }

    public function doupdateLink(){
    	$info = I('post.');
    	$map['lid'] = $info['lid'];
    	$data = array('title'=>$info['title'],'url'=>$info['url'],'status'=>$info['status'],'time'=>time());
    	$save = D('links')->where($map)->save($data);
    	if($save){
    		$this->success('修改友情链接成功！',$_SESSION['WKEIN_BACK_URL']);
       	}else{
            $this->error('修改友情链接失败！',$_SESSION['WKEIN_BACK_URL']);
       	}
    }

    public function delLink(){
    	$map['lid'] = I('get.lid');
    	$del = D('links')->where($map)->delete();
    	if($del){
    		$this->success('删除友情链接成功！',$_SESSION['WKEIN_BACK_URL']);
       	}else{
            $this->error('删除友情链接失败！',$_SESSION['WKEIN_BACK_URL']);
       	}
    }
	public function shenqing(){
		import('ORG.Util.Page');// 导入分页类
    	$count  = D('links')->where('sq = 1')->count();  // 查询满足要求的总记录数
        $Pages   = new Page($count,25);           // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show   = $Pages->show();                        // 分页显示输出
        $list = D('links')->where('sq = 1')->limit($Pages->firstRow.','.$Pages->listRows)->select();
		foreach($list as $k => &$v){
			
			$url = U('Link/status',array('lid'=>$v['lid'],'key'=>$v['status']));
			$name  = ($v['status'] == 1)?'开启':'关闭';
			$color = ($v['status'] == 1)?'green': 'red';
			$v['status'] = '<a href="'.$url.'" style="color:'.$color.'">'.$name.'</a>';
		}
		
	
		$_SESSION['WKEIN_BACK_URL'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		
        $this->assign('list',$list);                    // 赋值数据集
        $this->assign('page',$show);                    // 赋值分页输出
    	$this->show();
	}
	public function status(){
		$key = I('get.key');
		$map['lid'] = I('get.lid');
		$data['status'] =  1 - $key;
		$save = D('links')->where($map)->save($data);
		if($save){
    		$this->success('更改状态成功！',$_SESSION['WKEIN_BACK_URL']);
       	}else{
            $this->error('更改状态失败！',$_SESSION['WKEIN_BACK_URL']);
       	}
		
	}
}